﻿using Christ3D.Domain.Interfaces;
using Christ3D.Infrastruct.UoW;
using Dommel;
using Kogel.Dapper.Extension;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Christ3D.Infra.Data.Repository
{
    /// <summary>
    /// 泛型仓储，实现泛型仓储接口
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
    {
        protected DbSession _session;

        public Repository(DbSession session)
        {
            _session = session;
        }

        public void Add(TEntity obj)
        {
            _session.Connection.Insert<TEntity>(obj, _session.Transaction);
        }

        public TEntity GetById(Guid id)
        {
            return _session.Connection.Get<TEntity>(id, _session.Transaction);
        }

        public IEnumerable<TEntity> GetAll()
        {
            return _session.Connection.GetAll<TEntity>();
        }

        public void Update(TEntity obj)
        {
            _session.Connection.Update<TEntity>(obj, _session.Transaction);
        }

        public void Remove(Guid id)
        {
            _session.Connection.Delete<TEntity>(GetById(id));
        }

        public int SaveChanges()
        {
            _session.Transaction.Commit();
            return 1;
        }

        public void Dispose()
        {
            _session.Dispose();
            GC.SuppressFinalize(this);
        }


    }
}
